package testcases;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

import org.junit.Assert;

import com.trentech.billcalc.FinanceManager;
import com.trentech.billcalc.event.ArriveEvent;
import com.trentech.billcalc.event.BillEvent;
import com.trentech.billcalc.event.LeaveEvent;
import com.trentech.billcalc.event.OptInEvent;
import com.trentech.billcalc.event.OptOutEvent;
import com.trentech.billcalc.event.PayEvent;
import com.trentech.billcalc.event.RenewLeaseEvent;
import com.trentech.billcalc.event.SetupBillEvent;
import com.trentech.billcalc.event.StartLeaseEvent;
import com.trentech.billcalc.event.StartVacationEvent;
import com.trentech.billcalc.event.StopVacationEvent;
import com.trentech.billcalc.event.TransferLeaseEvent;


public class AddCompareDataTest {

    public static void AddEvents(FinanceManager fm) throws Exception 
    {
    	// Add SetupBill : GassBill,10
        SetupBill_Add(fm);
        // Add Resident : Marie Putri,-10; Alya,-3; Jessa,-2; 
        Arrive_Add(fm);
        // Add StartLease : Marie Putri,Jessa,3,80,20000,200,10000,Room Description New 
        StartLease_Add(fm);
        // Add Bill : Gass Bill,1,10000
        Bill_Add(fm);
        // Add Leave : Alya,20
        Leave_Add(fm);
        // Add Pay : Marie Putri,Jessa,2,10000
        Pay_Add(fm);
        // Add OptOut : Jessa,Gass Bill,20
        OptOut_Add(fm);
        // Add OptIn : Jessa,Gass Bill,25
        OptIn_Add(fm);
        // Add TransferLease : 
        //		Add Resident : Nexia,10
        // 		Add Leave : Jessa,10
        //		Add TransferLease : Jessa,Nexia,10 
        TransferLease_Add(fm);
        // Add RenewLease : Nexia,80,100
        RenewLease_Add(fm);
        // Add StartVacation : Marie Putri,10
        StartVacation_Add(fm);
        // Add StopVacation : Marie Putri,12
        StopVacation_Add(fm);
        
        
        //Leave_Exception(fm);
    }

    
    public static SetupBillEvent SetupBill_Add(FinanceManager fm) throws Exception {
        // Declare SetupBillEvent
        SetupBillEvent sbe = EventData.ListSetupBill.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(-8), sbe.getDate());
        Assert.assertEquals("Gass Bill", sbe.getName());
        Assert.assertTrue(sbe.isFixedCost());
        Assert.assertFalse(sbe.isAfterConsumption());

        // Do validation
        sbe.validate("name", fm);
        sbe.validate("date", fm);
    
        // Save event
        EventData.SaveEvent(fm, sbe);

        return sbe;
    }


    public static StartLeaseEvent StartLease_Add(FinanceManager fm) throws Exception {
        // Declare StartLeaseEvent
        StartLeaseEvent sle = EventData.ListStartLease.get(0);

        // Check values after modification
        Assert.assertEquals(20000, sle.getAmount());
        Assert.assertEquals(200, sle.getDeposit());
        Assert.assertEquals(10000, sle.getLeaseBreakPenalty());
        Assert.assertEquals(2, sle.getPaymentFrequency());
        Assert.assertEquals(MainTest.addDays(0), sle.getDate());
        Assert.assertEquals(MainTest.addDays(30), sle.getLeaseEnd());
        Assert.assertEquals("Marie Putri", sle.getName());
        Assert.assertEquals("Jessa", sle.getPayerName());
        Assert.assertEquals("Room Description New", sle.getRoomDescription());
        Assert.assertFalse(sle.isCanSublet());

        // Do validation
        sle.validate("date", fm);
        sle.validate("name", fm);
        sle.validate("amount", fm);
        sle.validate("payerName", fm);
        sle.validate("deposit", fm);
        sle.validate("leaseBreakPenalty", fm);
        sle.validate("paymentFrequency", fm);
        sle.validate("roomDescription", fm);
        sle.validate("leaseEnd", fm);

        // Save event
        EventData.SaveEvent(fm, sle);

        return sle;
    }


    public static void Arrive_Add(FinanceManager fm) throws Exception {
        // Declare ArriveEvent
        ArriveEvent av = EventData.ListArrive.get(0);

        // Check values
        Assert.assertEquals("Marie Putri", av.getName());
        Assert.assertEquals(MainTest.addDays(-10), av.getDate());

        // Do validation
        av.validate("name", fm);
        av.validate("date", fm);

        // Save event
        int residentNum = fm.getResidents().size();
        EventData.SaveEvent(fm, av);
        
        // Add other data
        EventData.SaveEvent(fm, EventData.ListArrive.get(1));
        EventData.SaveEvent(fm, EventData.ListArrive.get(2));
        
        assertEquals(residentNum+3, fm.getResidents().size());
    }


    public static void Leave_Add(FinanceManager fm) throws Exception {
        // Declare LeaveEvent 
        LeaveEvent lv = EventData.ListLeave.get(0);

        // Check values after modification
        Assert.assertEquals("Alya", lv.getName());
        Assert.assertEquals(MainTest.addDays(20), lv.getDate());

        // Do validation
        lv.validate("name", fm);
        lv.validate("date", fm);

        // Save event, number of residents will stay the same
        int residentNum = fm.getResidents().size();
        EventData.SaveEvent(fm, lv);
        assertEquals(residentNum, fm.getResidents().size());
    }
    
    
    public static void Leave_Exception(FinanceManager fm){
    	// Declare LeaveEvent
    	LeaveEvent lv = EventData.ListLeave.get(1);
    	
    	// Change name
    	lv.setName("Tom");
    	try
    	{
    		EventData.SaveEvent(fm, lv);
    	}
    	catch (IllegalStateException ex)
    	{
    		
    	}
    }


    public static BillEvent Bill_Add(FinanceManager fm) throws Exception {
        // Declare BillEvent
        BillEvent be = EventData.ListBill.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(1), be.getDate());
        Assert.assertEquals("Gass Bill", be.getName());
        Assert.assertEquals(12000, be.getAmount());

        // Do validation
        be.validate("name", fm);
        be.validate("date", fm);
        be.validate("amount", fm);

        // Save event
        EventData.SaveEvent(fm, be);

        return be;
    }


    public static PayEvent Pay_Add(FinanceManager fm) throws Exception {
        // Declare PayEvent
        PayEvent pe = EventData.ListPay.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(2), pe.getDate());
        Assert.assertEquals("Marie Putri", pe.getName());
        Assert.assertEquals(10000, pe.getAmount());
        Assert.assertEquals("Jessa", pe.getPaidEntity());

        // Do validation
        pe.validate("name", fm);
        pe.validate("date", fm);
        pe.validate("amount", fm);
        pe.validate("paidEntity", fm);

        // Save event
        EventData.SaveEvent(fm, pe);

        return pe;
    }


    public static OptOutEvent OptOut_Add(FinanceManager fm) throws Exception {
        // Declare OptOutEvent
        OptOutEvent ooe = EventData.ListOptOut.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(-2), ooe.getDate());
        Assert.assertEquals("Jessa", ooe.getName());
        Assert.assertEquals("Gass Bill", ooe.getBillerName());

        // Do validation
        ooe.validate("name", fm);
        ooe.validate("date", fm);
        ooe.validate("billerName", fm);

        // Save event
        EventData.SaveEvent(fm, ooe);

        return ooe;
    }


    public static OptInEvent OptIn_Add(FinanceManager fm) throws Exception {
        // Declare OptInEvent
        OptInEvent oie = EventData.ListOptIn.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(5), oie.getDate());
        Assert.assertEquals("Jessa", oie.getName());
        Assert.assertEquals("Gass Bill", oie.getBillerName());

        // Do validation
        oie.validate("date", fm);
        oie.validate("billerName", oie.getBillerName(), fm);
        oie.validate("name", oie.getName(), fm);

        // Save event
        EventData.SaveEvent(fm, oie);

        return oie;
    }


    public static TransferLeaseEvent TransferLease_Add(FinanceManager fm) throws Exception {
        // Before TransferEvent
    	EventData.SaveEvent(fm, EventData.ListArrive.get(4));
        EventData.SaveEvent(fm, EventData.ListLeave.get(1));

        // Declare TransferLeaseEvent
        TransferLeaseEvent tle = EventData.ListTransferLease.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(6), tle.getDate());
        Assert.assertEquals("Jessa", tle.getName());
        Assert.assertEquals("Nexia", tle.getNewTenant());
        Assert.assertEquals(true, tle.isTransferPaid());

        // Do validation
        tle.validate("date", fm);
        tle.validate("name", fm);
        tle.validate("newTenant", fm);

        // Save event
        EventData.SaveEvent(fm, tle);

        return tle;
    }


    public static RenewLeaseEvent RenewLease_Add(FinanceManager fm) throws Exception {
        // Declare RenewLeaseEvent
        RenewLeaseEvent rle = EventData.ListRenewLease.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(80), rle.getDate());
        Assert.assertEquals("Nexia", rle.getName());
        Assert.assertEquals(MainTest.addDays(100), rle.getLeaseEnd());

        // Do validation
        rle.validate("date", fm);
        rle.validate("name", fm);
        rle.validate("leaseEnd", fm);

        // Save event
        EventData.SaveEvent(fm, rle);

        return rle;
    }


    public static StartVacationEvent StartVacation_Add(FinanceManager fm) throws Exception {
        // Declare StartVacationEvent
        StartVacationEvent sve = EventData.ListStartVacation.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(6), sve.getDate());
        Assert.assertEquals("Marie Putri", sve.getName());

        // Do validation
        sve.validate("date", fm);
        sve.validate("name", fm);

        // Save event
        EventData.SaveEvent(fm, sve);

        return sve;
    }


    public static StopVacationEvent StopVacation_Add(FinanceManager fm) throws Exception {
        // Declare StopVacationEvent
        StopVacationEvent sve = EventData.ListStopVacation.get(0);

        // Check values
        Assert.assertEquals(MainTest.addDays(12), sve.getDate());
        Assert.assertEquals("Marie Putri", sve.getName());

        // Do validation
        sve.validate("date", fm);
        sve.validate("name", fm);

        // Save event
        EventData.SaveEvent(fm, sve);

        return sve;
    }

    

    public static void CompareEvents(FinanceManager fm)
    {   
        Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
        //Compare_Arrive(fm, EventData.ListArrive.get(0));
        Compare_StartLease(fm, EventData.ListStartLease.get(1));
        Compare_Bill(fm, EventData.ListBill.get(0));
        //Compare_Leave(fm, EventData.ListLeave.get(0));
        Compare_Pay(fm, EventData.ListPay.get(0));
        Compare_OptOut(fm, EventData.ListOptOut.get(0));
        Compare_OptIn(fm, EventData.ListOptIn.get(0));
        Compare_TransferLease(fm, EventData.ListTransferLease.get(0));
        Compare_RenewLease(fm, EventData.ListRenewLease.get(0));
        Compare_StartVacation(fm, EventData.ListStartVacation.get(0));
        Compare_StopVacation(fm, EventData.ListStopVacation.get(0));
        
    }


    public static void Compare_SetupBill(FinanceManager fm, SetupBillEvent sbe) {
        // Declare SetupBillEvent
        SetupBillEvent sbe2 = new SetupBillEvent(MainTest.addDays(0),
                "Telephone Bill", false, true);
        assertNotSame(0, sbe2.compareTo(sbe));

        sbe2.setDate(MainTest.addDays(-8));
        assertNotSame(0, sbe2.compareTo(sbe));

        sbe2.setName("Gass Bill");
        assertNotSame(0, sbe2.compareTo(sbe));

        sbe2.setFixedCost(true);
        assertNotSame(0, sbe2.compareTo(sbe));

        sbe2.setAfterConsumption(false);
        assertEquals(0, sbe2.compareTo(sbe));
    }


    public static void Compare_StartLease(FinanceManager fm, StartLeaseEvent sle) {
        // Declare StartLeaseEvent
        StartLeaseEvent sle2 = new StartLeaseEvent(MainTest.addDays(0),
                "LandLord", 10000, "Endok", MainTest.addDays(10), 4, 100, false,
                "Room Description", 5000);

        sle2.setDate(MainTest.addDays(2));
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setName("Marie Putri");
        assertNotSame(0, sle2.compareTo(sle));
        
        sle2.setAmount(12000);
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setPayerName("Alya");
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setRoomDescription("Room 223B");
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setCanSublet(true);
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setLeaseEnd(MainTest.addDays(20));
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setPaymentFrequency(2);
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setDeposit(400);
        assertNotSame(0, sle2.compareTo(sle));

        sle2.setLeaseBreakPenalty(7000);
        assertEquals(0, sle2.compareTo(sle));
    }


    public static void Compare_Bill(FinanceManager fm, BillEvent be) {
        // Declare BillEvent
        BillEvent be2 = new BillEvent(MainTest.addDays(-5), "Telephone Bill",
                20000);
        assertNotSame(0, be2.compareTo(be));

        be2.setDate(MainTest.addDays(1));
        assertNotSame(0, be2.compareTo(be));

        be2.setName("Gass Bill");
        assertNotSame(0, be2.compareTo(be));

        be2.setAmount(12000);
        assertEquals(0, be2.compareTo(be));
    }


    public static void Compare_Pay(FinanceManager fm, PayEvent pe) {
        // Declare PayEvent
        PayEvent pe2 = new PayEvent(MainTest.addDays(0), "Tom", 20000,
                "Jerry");
        assertNotSame(0, pe2.compareTo(pe));

        pe2.setDate(MainTest.addDays(2));
        assertNotSame(0, pe2.compareTo(pe));

        pe2.setName("Marie Putri");
        assertNotSame(0, pe2.compareTo(pe));

        pe2.setAmount(10000);
        assertNotSame(0, pe2.compareTo(pe));

        pe2.setPaidEntity("Jessa");
        assertEquals(0, pe2.compareTo(pe));
    }


    public static void Compare_OptOut(FinanceManager fm, OptOutEvent ooe) {
        // Declare OptOutEvent
        OptOutEvent ooe2 = new OptOutEvent(MainTest.addDays(15), "Marie Putri",
                "Telephone Bill");
        assertNotSame(0, ooe2.compareTo(ooe));

        ooe2.setDate(MainTest.addDays(-2));
        assertNotSame(0, ooe2.compareTo(ooe));

        ooe2.setName("Jessa");
        assertNotSame(0, ooe2.compareTo(ooe));

        ooe2.setBillerName("Gass Bill");
        assertEquals(0, ooe2.compareTo(ooe));
    }


    public static void Compare_OptIn(FinanceManager fm, OptInEvent oie) {
        // Declare OptInEvent
        OptInEvent oie2 = new OptInEvent(MainTest.addDays(15), "Marie Putri",
                "Telephone Bill");
        assertNotSame(0, oie2.compareTo(oie));

        oie2.setDate(MainTest.addDays(5));
        assertNotSame(0, oie2.compareTo(oie));

        oie2.setName("Jessa");
        assertNotSame(0, oie2.compareTo(oie));

        oie2.setBillerName("Gass Bill");
        assertEquals(0, oie2.compareTo(oie));
    }


    public static void Compare_TransferLease(FinanceManager fm, TransferLeaseEvent tle) {
        // Declare TransferLeaseEvent
        TransferLeaseEvent tle2 = new TransferLeaseEvent(MainTest.addDays(3),
                "Tom", "Jerry", false);
        assertNotSame(0, tle2.compareTo(tle));

        tle2.setDate(MainTest.addDays(6));
        assertNotSame(0, tle2.compareTo(tle));

        tle2.setName("Jessa");
        assertNotSame(0, tle2.compareTo(tle));

        tle2.setNewTenant("Nexia");
        assertNotSame(0, tle2.compareTo(tle));

        tle2.setTransferPaid(true);
        assertEquals(0, tle2.compareTo(tle));
    }
    

    public static void Compare_RenewLease(FinanceManager fm, RenewLeaseEvent rle) {
        // Declare RenewLeaseEvent
        RenewLeaseEvent rle2 = new RenewLeaseEvent(MainTest.addDays(3),
                "Jessa", MainTest.addDays(9));
        assertNotSame(0, rle2.compareTo(rle));

        rle2.setDate(MainTest.addDays(80));
        assertNotSame(0, rle2.compareTo(rle));

        rle2.setName("Nexia");
        assertNotSame(0, rle2.compareTo(rle));

        rle2.setLeaseEnd(MainTest.addDays(100));
        assertEquals(0, rle2.compareTo(rle));
    }
    

    public static void Compare_StartVacation(FinanceManager fm, StartVacationEvent sve) {
        // Declare StartVacationEvent
        StartVacationEvent sve2 = new StartVacationEvent(MainTest.addDays(3), "Lappie");
        assertNotSame(0, sve2.compareTo(sve));

        sve2.setDate(MainTest.addDays(6));
        assertNotSame(0, sve2.compareTo(sve));

        sve2.setName("Marie Putri");
        assertEquals(0, sve2.compareTo(sve));
    }


    public static void Compare_StopVacation(FinanceManager fm, StopVacationEvent sve) {
        // Declare StopVacationEvent
        StopVacationEvent sve2 = new StopVacationEvent(MainTest.addDays(3), "Lappie");
        assertNotSame(0, sve2.compareTo(sve));

        sve2.setDate(MainTest.addDays(12));
        assertNotSame(0, sve2.compareTo(sve));

        sve2.setName("Marie Putri");
        assertEquals(0, sve2.compareTo(sve));
    }
}
